package uk.co.q3c.deplan.server.unit.service;

import org.testng.Assert;
import org.testng.annotations.Test;

import uk.co.q3c.deplan.client.domain.resource.PersistenceReferenceException;
import uk.co.q3c.deplan.client.domain.task.BaseTask;
import uk.co.q3c.deplan.server.service.Leveller;
import uk.co.q3c.deplan.server.unit.domain.task.DomainBase_UT;

@Test
public class LevellerCandidate_UT extends DomainBase_UT {
	Leveller leveller;

	@Override
	protected void beforeMethod() throws Exception {
		super.beforeMethod();
		leveller = new Leveller(connection);
	}

	/**
	 * add a single atomic task as candidate, incomplete, no dependencies
	 * 
	 * @throws PersistenceReferenceException
	 */
	public void singleCandidate() throws PersistenceReferenceException {
		BaseTask at = createStandardTask("thing");
		at.setWorkRemaining(1);
		at.setAssignedResource(createResource("resource"));
		at.setAssignedUnits(1);
		leveller.addCandidate(at);
		Assert.assertEquals(leveller.candidateCount(), 1);
		leveller.processCandidates();
		Assert.assertEquals(leveller.activeCount(), 1);
		Assert.assertEquals(leveller.candidateCount(), 0);

	}

	/**
	 * add an incomplete single task as candidate, has a successor. Both tasks should be in the active list
	 * 
	 * test suspended
	 * 
	 * @throws PersistenceReferenceException
	 */
	@Test(enabled = false)
	public void singleCandidate_withSuccessor() throws PersistenceReferenceException {
		BaseTask at1 = createStandardTask("successor");
		BaseTask at = createStandardTask("thing");
		at.setAssignedResource(createResource("resource"));
		at.setAssignedUnits(1);
		at1.setAssignedResource(createResource("resource"));
		at1.setAssignedUnits(1);
		at.setWorkRemaining(1);
		at1.setWorkRemaining(1);

		at.addSuccessor(at1);
		at.save();

		leveller.addCandidate(at);
		Assert.assertEquals(leveller.candidateCount(), 1);
		leveller.processCandidates();
		Assert.assertEquals(leveller.activeCount(), 2);
		Assert.assertEquals(leveller.candidateCount(), 0);
	}

	/**
	 * Candidate rejected because no resource specified
	 * 
	 * @throws PersistenceReferenceException
	 * 
	 * @throws PersistenceReferenceException
	 */
	public void noResource() throws PersistenceReferenceException {
		BaseTask at = createStandardTask("thing");
		at.setWorkRemaining(1);
		leveller.addCandidate(at);
		Assert.assertEquals(leveller.candidateCount(), 1);
		leveller.processCandidates();
		Assert.assertEquals(leveller.activeCount(), 0);
		Assert.assertEquals(leveller.candidateCount(), 0);
	}

	/**
	 * Candidate rejected because no units specified
	 * 
	 * @throws PersistenceReferenceException
	 */
	public void noResourceUnits() throws PersistenceReferenceException {
		BaseTask at = createStandardTask("thing");
		at.setWorkRemaining(1);
		at.setAssignedResource(createResource("resource"));
		leveller.addCandidate(at);
		Assert.assertEquals(leveller.candidateCount(), 1);
		leveller.processCandidates();
		Assert.assertEquals(leveller.activeCount(), 0);
		Assert.assertEquals(leveller.candidateCount(), 0);
	}
}
